<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper" v-if="createFormBox">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="4" :sm="24" style="min-width: 230px;">
            <a-form-item>
              <a-input v-model="queryParam.id" placeholder="请输入试验ID" />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24" style="min-width: 230px;">
            <a-form-item>
              <a-select v-model="queryParam.status" placeholder="请选择试验类型" default-value="0">
                <a-select-option value="0">全部</a-select-option>
                <a-select-option value="1">关闭</a-select-option>
                <a-select-option value="2">运行中</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <div style="float: right;margin-right:24px;">
            <a-button type="primary" icon="plus" size="20" @click="createFormBox=false">创建试验</a-button>
          </div>
        </a-row>
      </a-form>
      <s-table
        class="table_style"
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        showPagination="auto">
        <span slot="serial" slot-scope="text,record,index">{{ index + 1 }}</span>
        <template slot="status" slot-scope="text,record,index" class="table_status_box">
          <a-popover title="可切换账户状态" trigger="click" class="table_status_title">
            <div slot="content">
              <a-button class="table_status table_status0" @click="tableStatusEvent(text)">预发布</a-button>
              <a-button class="table_status table_status1" @click="tableStatusEvent(text)">进行中</a-button>
              <br>
              <a-button class="table_status table_status2" @click="tableStatusEvent(text)">暂停中</a-button>
              <a-button class="table_status table_status3" @click="tableStatusEvent(text)">暂停中</a-button>
            </div>

            <a-button :class="'table_status'+text" @click="aaa" ref="tableStatusEvent">{{ designArray[text] }}</a-button>
          </a-popover>
        </template>
        <span slot="customTitle">发布时间
          <a-tooltip placement="top">
            <template slot="title">
              <span>最新版本发布时间</span>
            </template>
            <a-icon
              type="exclamation"
              :style="{ fontSize: '16px',
                        color: '#fff',
                        fontWeight: '700',
                        borderRadius: '50%',
                        marginLeft: '5px',
                        background: 'rgba(191,191,191,1)'}" />
          </a-tooltip>
        </span>
        <span slot="action" slot-scope="record" class="operation_button_box">
          <template>
            <a-button size="small" type="primary" @click="handleEdit(record)" class="operation_button_details">详情</a-button>
            <a-button v-if="false" size="small" class="operation_button_release" @click="handleSub(record)" ghost>发布新版</a-button>
            <a-button v-else size="small" class="operation_button_edit" type="danger" @click="handleSub(record)">编辑</a-button>
          </template>
        </span>
      </s-table>
    </div>
    <!-- <create-form v-else @ok="handleOk" @Cancel="CreateFormHandleCancel" />
    <test-design @ok="handleOk"  ref="modal" ></test-design>
    <step-by-step-modal ref="modal" @ok="handleOk" /> -->
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
// import TestDesign from './modules/TestDesign'
// import CreateForm from './modules/CreateForm'
import { getRoleList, getServiceList } from '@/api/manage'

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis
    // CreateForm,
    // TestDesign
  },
  data () {
    return {
      createFormBox: true,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      visible: false,
      designArray: {
        '0': '预发布',
        '1': '进行中',
        '2': '暂停中',
        '3': '已结束'
      },
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          align: 'center'
        },
        {
          title: '试验ID',
          dataIndex: 'no',
          align: 'center'
        },
        {
          title: '试验名称',
          dataIndex: 'description',
          align: 'center'
        },
        {
          title: '试验类型',
          dataIndex: 'callNo',
          needTotal: true,
          customRender: (text) => text + ' 次',
          align: 'center'
        },
        // {
        //   title: '项目负责人',
        //   dataIndex: 'status',
        //   align: 'center'
        // },
        // {
        //   title: '申办方',
        //   dataIndex: 'description',
        //   align: 'center'
        // },
        {
          title: '试验状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          align: 'center'
        },
        {
          // title: '发布时间',
          // dataIndex: 'updatedAt',
          // align:'center'

          dataIndex: 'updatedAt',
          key: 'updatedAt',
          slots: { title: 'customTitle' },
          scopedSlots: { customRender: 'updatedAt' },
          // sorter: true,
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '200px',
          scopedSlots: { customRender: 'action' },
          align: 'center'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        return getServiceList(Object.assign(parameter, this.queryParam))
          .then(res => {
            console.log(res.result, 'shuj')
            return res.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  created () {
    this.tableOption()
    getRoleList({ t: new Date() })
  },
  methods: {
    CreateFormHandleCancel () {
      this.createFormBox = true
    },
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: record.no === 'No 2', // Column configuration not to be checked
                name: record.no
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },

    handleEdit (record) {
      console.log(record)
      this.$refs.modal.edit(record)
    },
    handleSub (record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    },
    tableStatusEvent (v) {
    },
    aaa () {

    }
  }
}
</script>

<style lang="less">
.table_status {
  margin: 5px;
}
.ant-popover-title {
  width: 98px;
  height: 22px;
  font-size: 14px;
  font-family: PingFangSC-Regular, Microsoft Yahei, sans-serif;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  line-height: 22px;
}
.table_status0 {
  border: 1px #999999 solid;
  span {
    color: #999999;
  }
  &:hover {
    border: 1px #999999 solid !important;
    background: #999999;
    span {
      color: #000;
    }
  }
}

.table_status1 {
  border: 1px #56ced1 solid;
  span {
    color: #56ced1;
  }
  &:hover {
    border: 1px #56ced1 solid !important;
    background: #56ced1;
    span {
      color: #fff;
    }
  }
}
.table_status2 {
  border: 1px #eea91c solid;
  span {
    color: #eea91c;
  }
  &:hover {
    border: 1px #eea91c solid !important;
    background: #eea91c;
    span {
      color: #fff;
    }
  }
}
.table_status3 {
  border: 1px #c42525 solid;
  span {
    color: #c42525;
  }
  &:hover {
    border: 1px #c42525 solid !important;
    background: #c42525;
    span {
      color: #fff;
    }
  }
}
.ant-popover-inner-content {
  padding: 5px 10px;
}
.table_status_box:hover {
  background: red;
}
.popover_hint {
  background: red;
}
.operation_button_box {
  padding: 0px;
}
.operation_button_details {
  width: 74px;
  height: 32px;
}
.operation_button_release {
  width: 74px;
  height: 32px;
  margin-left: 10px;
  border: 1px #56ced1 solid;
}
.operation_button_release span {
  color: #56ced1;
}
.operation_button_release:hover {
  background: #c9eced;
  opacity: 0.8;
  border-color: #c9eced;
}
.operation_button_edit {
  width: 74px;
  height: 32px;
  margin-left: 10px;
  background: #eea91c;
}
.operation_button_edit span {
  color: #fff;
}
.operation_button_edit:hover {
  background: #eea91c;
  opacity: 0.8;
  border-color: #eea91c;
}
</style>
